//
//  JsonFileIO.m
//  4GCare
//
//  Created by Zhou, Shaolin on 12/21/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "JsonFileIO.h"

@implementation JsonFileIO

@synthesize fileName;

-(id)initFileName:(NSString*)name
{
	if ((self = [super init])) {
		self.fileName = name;
	}
	return self;
}

- (NSString *)jsonFilePath{
	NSString *documentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
	return [documentFolder stringByAppendingPathComponent: fileName];
}

- (NSMutableDictionary *)readDataToDict{
    NSMutableDictionary* jsonDict = [[[NSMutableDictionary alloc] initWithContentsOfFile:self.jsonFilePath] autorelease];
	
	if (!jsonDict) {
		jsonDict = [[[NSMutableDictionary alloc] init] autorelease];
	}
	
	return jsonDict;
}

- (NSMutableArray *)readDataToArray{
	NSMutableArray* jsonArray = [[[NSMutableArray alloc] initWithContentsOfFile:self.jsonFilePath] autorelease];
	
	if (!jsonArray) {
		jsonArray = [[[NSMutableArray alloc] init] autorelease];
	}
	
	return jsonArray;
}

- (BOOL)saveArray:(NSArray*)array
{
	BOOL bSuccessed = [array writeToFile:self.jsonFilePath atomically:YES];
	return bSuccessed;
}

- (BOOL)saveDict:(NSDictionary*)dict
{
	BOOL bSuccessed = [dict writeToFile:self.jsonFilePath atomically:YES];
	return bSuccessed;
}

-(void)dealloc
{
    [fileName release];
    [super dealloc];
}

@end
